package com.corx.network.core.model;

import com.corx.network.core.type.NetworkTypeEnum;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author gx
 * @version 1.0.0
 */
public class ReceivedMessage extends MessageBase {

    /**
     * 发送方标识（设备 ID / 客户端 ID / topic / ip:port）
     */
    private String source;

    /**
     * 业务类型/逻辑路由
     */
    private String route;

    /**
     * 接收时间戳
     */
    private Long timestamp;

    /**
     * 是否为响应消息（可选）
     */
    private Boolean reply;

    /**
     * 响应状态码（可选）
     */
    private Integer status;

    /**
     * 响应错误信息（可选）
     */
    private String error;

    /**
     * 翻译后的消息体（可选）
     */
    private String translatedPayload;

    public ReceivedMessage() {
    }

    private ReceivedMessage(Builder builder) {
        super( builder.deviceId, builder.messageId, builder.networkType , builder.payload, builder.headers);
        this.source = Objects.requireNonNull(builder.source, "source cannot be null");
        this.route = builder.route;
        this.timestamp = builder.timestamp;
        this.reply = builder.reply;
        this.status = builder.status;
        this.error = builder.error;
        this.translatedPayload = builder.translatedPayload;
    }


    public static Builder builder(String source, byte[] payload) {
        return new Builder(source, payload);
    }

    public static class Builder {
        private String deviceId;
        private String messageId;
        private NetworkTypeEnum networkType;
        private final String source;
        private final byte[] payload;
        private Map<String, String> headers = new HashMap<>(1);
        private String route = "";
        private Long timestamp = System.currentTimeMillis();
        private Boolean reply = false;
        private Integer status = 0;
        private String error;
        private String translatedPayload;

        public Builder(String source, byte[] payload) {
            this.source = source;
            this.payload = payload;
        }

        public Builder networkType(NetworkTypeEnum networkType) {
            this.networkType = networkType;
            return this;
        }

        public Builder deviceId(String deviceId) {
            this.deviceId = deviceId;
            return this;
        }

        public Builder messageId(String messageId) {
            this.messageId = messageId;
            return this;
        }

        public Builder route(String route) {
            this.route = route;
            return this;
        }

        public Builder timestamp(Long timestamp) {
            this.timestamp = timestamp;
            return this;
        }

        public Builder reply(Boolean reply) {
            this.reply = reply;
            return this;
        }

        public Builder status(Integer status) {
            this.status = status;
            return this;
        }

        public Builder error(String error) {
            this.error = error;
            return this;
        }

        public Builder translatedPayload(String translatedPayload) {
            this.translatedPayload = translatedPayload;
            return this;
        }

        public Builder headers(Map<String, String> headers) {
            this.headers = headers;
            return this;
        }

        public ReceivedMessage build() {
            return new ReceivedMessage(this);
        }
    }



    public String getSource() {
        return source;
    }

    public void setSource(String source) {
        this.source = source;
    }

    public String getRoute() {
        return route;
    }

    public void setRoute(String route) {
        this.route = route;
    }

    public Long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(Long timestamp) {
        this.timestamp = timestamp;
    }

    public Boolean getReply() {
        return reply;
    }

    public void setReply(Boolean reply) {
        this.reply = reply;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public String getError() {
        return error;
    }

    public void setError(String error) {
        this.error = error;
    }

    public String getTranslatedPayload() {
        return translatedPayload;
    }

    public void setTranslatedPayload(String translatedPayload) {
        this.translatedPayload = translatedPayload;
    }

    @Override
    public String toString() {
        return "ReceivedMessage{" +
            "deviceId='" + deviceId + '\'' +
            ", networkType=" + networkType +
            ", messageId='" + messageId + '\'' +
            ", headers=" + headers +
            ", payload=" + Arrays.toString(payload) +
            ", source='" + source + '\'' +
            ", route='" + route + '\'' +
            ", timestamp=" + timestamp +
            ", reply=" + reply +
            ", status=" + status +
            ", error='" + error + '\'' +
            ", translatedPayload='" + translatedPayload + '\'' +
            '}';
    }
}
